﻿using System;
using PrimeTween ;
using UnityEngine;

namespace UI
{
    [RequireComponent(typeof(CanvasGroup))]
    public class UiFader : MonoBehaviour
    {
        [SerializeField] private float fadeTime = 0.5f;
        [SerializeField] private bool hideOnStart = false;
        
        private CanvasGroup _canvasGroup;


        private void Awake()
        {
            _canvasGroup = GetComponent<CanvasGroup>();
            if (hideOnStart)
            {
                _canvasGroup.alpha = 0f;
            }
        }

        public void Show()
        {
            Tween.Alpha(_canvasGroup, 1f, fadeTime, useUnscaledTime: true);
            _canvasGroup.blocksRaycasts = true;
        }

        public void Hide()
        {
            Tween.Alpha(_canvasGroup, 0f, fadeTime, useUnscaledTime: true);
            _canvasGroup.blocksRaycasts = false;
        }
    }
}